import flask
from flask import Flask,request
from todo import Todo
from todomanager_mysql import Todomanage_mysql

a = Todomanage_mysql()
app = Flask(__name__)
app.config['JSON_AS_ASCII']=False
a.create_table()
@app.route('/')
def respon():
    return '<h1>hello world</h1>'
@app.route('/todo',methods = ['GET'])
def get_todo():
    return {'todo':[x.__dict__ for x in a.get_list()]}
@app.route('/create_todo',methods=['POST'])
def created():
    if 'title' not in request.json:
        return '参数错误，必须提供title参数',400
    todo = a.create(request.json['title'])
    return todo.__dict__
@app.route('/mark_completed',methods=['GET'])
def change():
    if 'id' not in request.args:
        return '参数错误，必须提供id参数',400
    data = a.mark_completed(int(request.args['id']))
    return data.__dict__
@app.route('/delete_todo',methods=['GET'])
def delete():
    if 'id' not in request.args:
        return '参数错误，必须提供title参数',400
    todo = a.delete(int(request.args['id']))
    return todo.__dict__
if __name__ == '__main__':
    app.run()


